package es.unileon.is.finalproyect.userinterface;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;

import es.unileon.is.finalproyect.exceptions.ApplicationException;
import es.unileon.is.finalproyect.exceptions.UniqueException;
import es.unileon.is.finalproyect.users.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RegisterInterface extends JFrame {
	
	private JTextField txtUser;
	private JTextField txtPassword;
	private JTextField txtConfirmPassword;
	private JTextField txtName;
	private JTextField txtSurname;
	private JTextField txtEmail;
	private JTextField txtDay;
	private JTextField txtMonth;
	private JTextField txtYear;
	private JComboBox comboBox;
	private JPanel _contentPane;
	private JPasswordField pwdPassword;
	private JPasswordField pwdConfirmPassword;
	private Date _dateOfBirth=null;
	/**
	 * Create the frame.
	 */
	public RegisterInterface() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(10, 10, 400, 300);
		_contentPane = new JPanel();
		_contentPane.setLayout(null);
		setContentPane(_contentPane);
		
		//*****modificaciones para JAVAHELP*******
		JMenuBar menuBar = new JMenuBar();
		JMenu menuHelp = new JMenu("Help");
		JMenuItem help = new JMenuItem("Show help");
		menuBar.add(Box.createHorizontalGlue());
		menuBar.add(menuHelp);
		menuHelp.add(help);
		setJMenuBar(menuBar);
		//metodo para dar la accion a help
		ViewHelp.showHelp(help, getContentPane(), "ventana_registro");
		//*****************************************
		
		FocusListener fl = new FocusAdapter(){
			/* Selecciona todo el texto al obtener el foco */
			public void focusGained(FocusEvent evt){
			    ((JTextField)evt.getSource()).selectAll();
			}
		    };
		
		txtUser= new JTextField();
		txtUser.setText("User");
		txtUser.setBounds(54, 11, 86, 20);
		getContentPane().add(txtUser);
		txtUser.setColumns(10);
		txtUser.addFocusListener(fl);
		
		txtPassword = new JTextField();		
		txtPassword.setText("Password");
		txtPassword.setBounds(54, 42, 86, 20);
		getContentPane().add(txtPassword);
		txtPassword.setColumns(10);
		pwdPassword = new JPasswordField();
		txtPassword.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent evt1) {
			    ((JTextField)evt1.getSource()).selectAll();
				pwdPassword.setToolTipText("Password");
				pwdPassword.setBounds(54, 42, 86, 20);
				getContentPane().add(pwdPassword);
				getContentPane().remove(txtPassword);
			}
		});
		
		txtConfirmPassword = new JTextField();		
		txtConfirmPassword.setText("Confirm Password");
		txtConfirmPassword.setBounds(150, 42, 111, 20);
		getContentPane().add(txtConfirmPassword);
		txtConfirmPassword.setColumns(10);
		pwdConfirmPassword = new JPasswordField();
		pwdConfirmPassword.setToolTipText("Confirm Password");
		txtConfirmPassword.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent evt2) {
			    ((JTextField)evt2.getSource()).selectAll();
				pwdConfirmPassword.setBounds(150, 42, 111, 20);
				getContentPane().add(pwdConfirmPassword);
				getContentPane().remove(txtConfirmPassword);
			}
		});
		
		txtName = new JTextField();		
		txtName.setText("Name");
		txtName.setBounds(54, 73, 86, 20);
		getContentPane().add(txtName);
		txtName.setColumns(10);
		txtName.addFocusListener(fl);
		
		txtSurname = new JTextField();		
		txtSurname.setText("Surname");
		txtSurname.setBounds(54, 104, 86, 20);
		getContentPane().add(txtSurname);
		txtSurname.setColumns(10);
		txtSurname.addFocusListener(fl);
		
		txtEmail = new JTextField();		
		txtEmail.setText("email");
		txtEmail.setBounds(54, 166, 129, 20);
		getContentPane().add(txtEmail);
		txtEmail.setColumns(10);
		txtEmail.addFocusListener(fl);
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Male", "Female"}));
		comboBox.setSelectedIndex(0);
		comboBox.setMaximumRowCount(2);
		comboBox.setBounds(113, 135, 70, 20);
		getContentPane().add(comboBox);
				
		JLabel lblGenre = new JLabel("Genre");
		lblGenre.setBounds(55, 135, 41, 20);
		getContentPane().add(lblGenre);
		
		txtDay = new JTextField();		
		txtDay.setText("Day");
		txtDay.setBounds(50, 197, 27, 20);
		getContentPane().add(txtDay);
		txtDay.setColumns(10);
		txtDay.addFocusListener(fl);
		
		txtMonth = new JTextField();		
		txtMonth.setText("Month");
		txtMonth.setColumns(10);
		txtMonth.setBounds(87, 197, 38, 20);
		getContentPane().add(txtMonth);
		txtMonth.addFocusListener(fl);
		
		txtYear = new JTextField();		
		txtYear.setText("Year");
		txtYear.setColumns(10);
		txtYear.setBounds(135, 197, 48, 20);
		getContentPane().add(txtYear);
		txtYear.addFocusListener(fl);
		
		JButton btnRegister = new JButton("Register");	
		btnRegister.setBounds(225, 123, 111, 44);
		getContentPane().add(btnRegister);


		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				parseLabels();
			}
		});
		
	}
	
	private void parseLabels(){
		if((txtUser.getText()==null)||(txtName.getText()==null)||(txtSurname.getText()==null)||(txtEmail.getText()==null)||(txtDay.getText()==null)||(txtMonth.getText()==null)||(txtYear.getText()==null)||(String.valueOf(pwdPassword.getPassword()).equals(""))||(String.valueOf(pwdConfirmPassword.getPassword()).equals(""))){
			JOptionPane.showMessageDialog(getContentPane(), "All fields must be filled", "Error", JOptionPane.ERROR_MESSAGE);
		}else if(!String.valueOf(pwdPassword.getPassword()).equals(String.valueOf(pwdConfirmPassword.getPassword()))){
			//MostrarMensaje de error y que vuelva a introducir contrase�a.
			JOptionPane.showMessageDialog(getContentPane(), "Passwords not equals", "Error", JOptionPane.ERROR_MESSAGE);
		}else{
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			try {
				_dateOfBirth=format.parse(txtYear.getText()+txtMonth.getText()+txtDay.getText());
			} catch (ParseException e) {
				JOptionPane.showMessageDialog(getContentPane(), "Incorrect Format in Date of Birth, please correct (yyyy mm dd)", "Error", JOptionPane.ERROR_MESSAGE);
				return;
			}
			int i = comboBox.getSelectedIndex();
			char genre;
			if(i==1){
				genre='M';
			}else {
				genre ='F';
			}
			try{
				new User(txtUser.getText(),String.valueOf(pwdPassword.getPassword()),txtName.getText(),txtSurname.getText(),_dateOfBirth,genre,txtEmail.getText());
				JOptionPane.showMessageDialog(getContentPane(), "Please, wait an admin to confirm your account", "Information", JOptionPane.INFORMATION_MESSAGE);
				dispose();
			} catch(ApplicationException e){
				JOptionPane.showMessageDialog(getContentPane(), e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}	
		}
	}
}